﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.Protocol;
using ServerPublic.Data;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using UtilPublic.ServerLoger;
using static UtilPublic.Serialization.Jsh;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.DataBaseRedis.RdsTxt;
using ServerPublic.Manager;
using ServerBase.Service;
using UtilPublic.Serialization;

namespace ServerGame.GameObject
{
    /// <summary>
    /// 玩家数据
    /// </summary>
    partial class Player
    {
        [Desc("基本")]
        [DataMember]
        public DataBasic DaBasic = new DataBasic();
        [Desc("心跳更新数据")]
        [DataMember]
        public DataLoop DaLoop = new DataLoop();
        [Desc("杂项")]
        [DataMember]
        public DataMisc DaMisc = new DataMisc();
        [Desc("邮件")]
        [DataMember]
        public DataMail DaMail = new DataMail();

        [Desc("游戏")]
        [DataMember]
        public DataCommon DaCommon = new DataCommon();

        [Desc("临时")]
        [DataMember]
        public DataTemp DaTemp = new DataTemp();


        // 读取玩家数据
        public bool Load()
        {
            loger.Debug($"Player.Load，玩家ID：{Uid}");
            try
            {
                PubDataMethodMgr.LoadPlayerMod(Uid, ref DaBasic, nameof(DaBasic));
                PubDataMethodMgr.LoadPlayerMod(Uid, ref DaLoop, nameof(DaLoop));
                PubDataMethodMgr.LoadPlayerMod(Uid, ref DaMisc, nameof(DaMisc));
                PubDataMethodMgr.LoadPlayerMod(Uid, ref DaMail, nameof(DaMail));
                PubDataMethodMgr.LoadPlayerMod(Uid, ref DaCommon, nameof(DaCommon));
                return true;
            }
            catch (Exception ex)
            {
                loger.Debug($"Player.Load 失败 玩家ID：{Uid}", ex);
                return false;
            }
        }

        // 存储玩家数据
        public bool Save()
        {
            loger.Debug($"Player.Save，玩家PUID：{Uid} 玩家名字：{Name}");

            bool result = true;
            try
            {
                //方便查看异常数据，分开写入字典
                var datas = new Dictionary<string, object>();

                datas[nameof(DaBasic)] = Jsh.Jstr(DaBasic);
                datas[nameof(DaLoop)] = Jsh.Jstr(DaLoop);
                datas[nameof(DaMisc)] = Jsh.Jstr(DaMisc);
                datas[nameof(DaMail)] = Jsh.Jstr(DaMail);
                datas[nameof(DaCommon)] = Jsh.Jstr(DaCommon);

                AHashSetDict($"{RdsPlayerData}:{Uid}", datas);


                if (!result)
                {
                    loger.Fatal($"Player.Save，失败：玩家PUID：{Uid} 玩家账号：{Name}");
                }

                return result;
            }
            catch (Exception ex)
            {
                loger.Fatal($"Player.Save，失败：玩家PUID：{Uid} 玩家账号：{Name}", ex);
                return false;
            }
        }

        // 新建玩家存储
        public bool SaveNewbie()
        {
            AHashSet(RdsPlayerPuidName, Uid, Name);
            AHashSet(RdsPlayerNamePuid, Name, Uid);

            //用户在不同服务器的Puid User:Puid:<Uuid> -- ConfigId -- Puid
            PubUserMgr.SaveUuidPuid(DaBasic.Uuid, BaseServerInfo.ConfigId, Uid);

            return Save();
        }
        public void SaveOnline()
        {
            if (IsOnline)
            {
                Save();
            }
        }
        public void SaveOffline()
        {
            if (!IsOnline)
            {
                Save();
            }
        }

    }

}